/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.service.impl;

import com.workcell.workdata.im.component.redis.MomentListRedisTemplate;
import com.workcell.workdata.im.entity.MomentRule;
import com.workcell.workdata.im.repository.MomentRuleRepository;
import com.workcell.workdata.im.service.MomentRuleService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MomentRuleServiceImpl implements MomentRuleService {

	@Resource
	private MomentRuleRepository momentRuleRepository;

	@Resource
	private MomentListRedisTemplate momentListRedisTemplate;

	@Override
	public void delete(@RequestBody MomentRule rule) {
		momentRuleRepository.delete(rule.getUid(),rule.getTargetId(),rule.getType());
		if (rule.getType() == MomentRule.TYPE_BLOCK){
			momentListRedisTemplate.block(rule.getUid(),rule.getTargetId(),false);
		}

		if (rule.getType() == MomentRule.TYPE_IGNORE){
			momentListRedisTemplate.ignore(rule.getUid(),rule.getTargetId(),false);
		}
	}

	@Override
	public boolean isBlacked(Long uid, Long targetId) {
		return momentRuleRepository.count(uid,targetId,MomentRule.TYPE_BLOCK) > 0;
	}

	@Override
	public boolean isIgnored(Long uid, Long targetId) {
		return momentRuleRepository.count(uid,targetId,MomentRule.TYPE_IGNORE) > 0;
	}

	@Override
	public void save(@RequestBody MomentRule rule) {
		momentRuleRepository.saveAndFlush(rule);
		if (rule.getType() == MomentRule.TYPE_BLOCK){
			momentListRedisTemplate.block(rule.getUid(),rule.getTargetId(),true);
		}

		if (rule.getType() == MomentRule.TYPE_IGNORE){
			momentListRedisTemplate.ignore(rule.getUid(),rule.getTargetId(),true);
		}
	}

	@Override
	public List<MomentRule> queryList(Long uid) {
		return momentRuleRepository.findList(uid);
	}

	@Override
	public List<Long> findNonNotifiableList(Long uid) {
		List<Long> idList = findMeBlockedList(uid);
		idList.addAll(findIgnoreMeList(uid));
		return idList;
	}

	@Override
	public List<Long> findInvisibleList(Long uid) {
		List<Long> idList = findBlockedMeList(uid);
		idList.addAll(findMeIgnoreList(uid));
		return idList;
	}

	@Override
	public List<Long> findMeBlockedList(Long uid) {
		List<Long> idList = momentListRedisTemplate.findMeBlockList(uid);
		if (idList.isEmpty()){
			idList.addAll(momentRuleRepository.findMeBlockedList(uid));
			momentListRedisTemplate.addMeBlockList(uid,idList);
		}
		return idList;
	}

	@Override
	public List<Long> findMeIgnoreList(Long uid) {
		List<Long> idList = momentListRedisTemplate.findMeIgnoreList(uid);
		if (idList.isEmpty()){
			idList.addAll(momentRuleRepository.findMeIgnoreList(uid));
			momentListRedisTemplate.addMeIgnoreList(uid,idList);
		}
		return idList;
	}

	@Override
	public List<Long> findBlockedMeList(Long uid) {
		List<Long> idList = momentListRedisTemplate.findBlockMeList(uid);
		if (idList.isEmpty()){
			idList.addAll(momentRuleRepository.findBlockedMeList(uid));
			momentListRedisTemplate.addBlockMeList(uid,idList);
		}
		return idList;
	}

	@Override
	public List<Long> findIgnoreMeList(Long uid) {
		List<Long> idList = momentListRedisTemplate.findIgnoreMeList(uid);
		if (idList.isEmpty()){
			idList.addAll(momentRuleRepository.findIgnoreMeList(uid));
			momentListRedisTemplate.addIgnoreMeList(uid,idList);
		}
		return idList;
	}

}
